//
//  HMTool.m
//  仿真项目开发
//
//  Created by B.HOPE on 2017/3/22.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMTool.h"

@implementation HMTool


+ (instancetype)sharedNetworkTool{
    static HMTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc]init];
        
        instance.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    });
    
    return instance;
}

- (void)GETWithURLString:(NSString *)URLString parameters:(id)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}

- (void)POSTWithURLString:(NSString *)URLString parameters:(id)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}

#pragma mark - 更新／设置当前用户模型
+ (void)updateCurrentIDModel:(HMLoginModel *)loginModel{
    
    //获取当前用户id
    NSString *name = [HMDefaultsTool objetForKey:@"currentID"];
    
    if (loginModel && name) {
        
        //将loginModel类型变为NSData类型
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:loginModel];
        
        //更新本地数据
        [HMDefaultsTool saveObject:data forKey:[NSString stringWithFormat:@"%@-MODEL",name]];
    }
    
}

+ (HMLoginModel *)getCurrentIDModel;{
    
    //获取当前用户id
    NSString *name = [HMDefaultsTool objetForKey:@"currentID"];
    //创建DBJLoginModel
    HMLoginModel *model = nil;
    //判断name是否nil
    if (name) {
        NSData *modelData = [HMDefaultsTool objetForKey:[NSString stringWithFormat:@"%@-MODEL",name]];
        model = [NSKeyedUnarchiver unarchiveObjectWithData:modelData];
        NSLog(@"DBJLoginModel: %@",model);
    }
    
    return model;
}


@end
